package com.le.buddha.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.le.buddha.mapper.ChatHistoryMapper;
import com.le.buddha.mobile.ChatHistory;
import com.le.buddha.mobile.vo.ChatHistoryVO;
import com.le.buddha.service.IChatHistoryService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements IChatHistoryService {

    @Override
    public ChatHistory queryChatLastHistoryByUser(Long userId) {
        ChatHistory chatHistory= this.lambdaQuery().eq(ChatHistory::getUserId,userId).last("order by create_date desc limit 1").one();
        return chatHistory;
    }

    @Override
    public String queryChatHistorySessionId(Long userId) {
        ChatHistory chatHistory= this.lambdaQuery().eq(ChatHistory::getUserId,userId).last("order by create_date desc limit 1").one();
        return ObjectUtil.isNotNull(chatHistory)?chatHistory.getSessionId(): UUID.randomUUID().toString().replace("-", "");
    }

    @Override
    public void saveChatHistory(Long userId, String question, String answer, String sessionId) {
        ChatHistory chatHistory=new ChatHistory();
        chatHistory.setUserId(userId);
        chatHistory.setQuestion(question);
        chatHistory.setAnswer(answer);
        chatHistory.setSessionId(sessionId);
        chatHistory.setCreateDate(new Date());
        this.save(chatHistory);

    }

    @Override
    public List<ChatHistoryVO> queryUserHistoryList(Long userId) {
       return this.baseMapper.queryUserHistoryList(userId);
    }
}
